﻿using MySqlConnector;

namespace Front._1125;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using var connection =
            new MySqlConnection(
                "server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "SELECT * FROM admin_info where admin_account = @admin_account and admin_passpord = ?";
        string username = this.textBox1.Text;
        string pwd = this.textBox2.Text;
        pwd = MD5Helper.GetMD5(pwd);
        using var command = new MySqlCommand(sql, connection);
        command.Parameters.AddWithValue("@admin_account", username);
        command.Parameters.AddWithValue("@pwd", pwd);
        using var reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            MainForm main = new MainForm();
            main.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show(text: "用户名或密码错误");
            textBox1.Text = "";
            textBox2.Text = "";
        }
    }

    private void Login_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
    }
}